
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "user_uart_serial.h"
#include "user_pc_drive.h"
#include "user_fushi_drive.h"

#define USER_RECVBUFF_LEN (256)
static uint8_t pc_recv_flag=0;


static USER_UART_SERIAL_T user_pc_serial_handle;

static uint8_t user_pc_recvbuff[USER_RECVBUFF_LEN];
static uint8_t user_pc_recvbuff_tmp[USER_RECVBUFF_LEN];

void user_pc_drive_init(void)
{
	memset(user_pc_recvbuff,0, sizeof(user_pc_recvbuff));
	
	user_pc_serial_handle.user_recvstart=0;
	user_pc_serial_handle.user_recvend=0;
	user_pc_serial_handle.user_recvbuff=user_pc_recvbuff;
	user_pc_serial_handle.user_recvbuff_len=sizeof(user_pc_recvbuff);
	user_uart_serial_init(&user_pc_serial_handle);
}
void user_pc_drive_send(UART_HandleTypeDef *huart,USER_CENTRAL_CONTROL_STATUS_T *status)
{
	uint16_t crc_tmp;
	switch(pc_recv_flag)
	{
		case 0x01:
		{
			HAL_GPIO_WritePin(DIR_G1_GPIO_Port, DIR_G1_Pin, GPIO_PIN_RESET);
			status->central_control_payloadbuff[0]=0x55;
			status->central_control_payloadbuff[1]=0xaa;
			status->central_control_payloadbuff[2]=status->central_control_address;
			status->central_control_payloadbuff[3]=status->central_control_payloadbuff_len+3;
			status->central_control_payloadbuff[4]=0x01;
			crc_tmp=CRC16 ((const uint8_t *)status->central_control_payloadbuff, status->central_control_payloadbuff_len+5);
			status->central_control_payloadbuff[status->central_control_payloadbuff_len+5]=crc_tmp/256;
			status->central_control_payloadbuff[status->central_control_payloadbuff_len+6]=crc_tmp%256;
			HAL_UART_Transmit(huart , (uint8_t *)status->central_control_payloadbuff, status->central_control_payloadbuff_len+7, 2000);
			HAL_GPIO_WritePin(DIR_G1_GPIO_Port, DIR_G1_Pin, GPIO_PIN_SET);
			pc_recv_flag=0;
		}
			
		break;
		case 0x02:
			pc_recv_flag=0;
		break;
		case 0x03:
			pc_recv_flag=0;
		break;
		case 0x04:
			pc_recv_flag=0;
		break;
		case 0x05:
			pc_recv_flag=0;
		break;
		case 0x06:
			pc_recv_flag=0;
		break;
		case 0x07:
			pc_recv_flag=0;
		break;
		
	}
}

void user_pc_drive_add_data(uint8_t data)
{
	user_uart_serial_add_data(&user_pc_serial_handle,data);
}

uint8_t user_pc_drive_handle(void)
{
	uint16_t data_recv_len_tmp=0;
	uint16_t data_recv_payload_len_tmp=0;
	uint16_t crc_tmp=0;
	USER_CENTRAL_CONTROL_STATUS_T*  tmp=user_central_control_get_status();
	//printf("pc %d\r\n",user_uart_serial_get_datalength(&user_pc_serial_handle));
	if(user_uart_serial_get_datalength(&user_pc_serial_handle)>=5)
	{
		memset(user_pc_recvbuff_tmp,0,sizeof(user_pc_recvbuff_tmp));
		do
		{
			data_recv_len_tmp=0;
			data_recv_len_tmp=user_uart_serial_check_data(&user_pc_serial_handle,user_pc_recvbuff_tmp,1);
			if(user_pc_recvbuff_tmp[0]!=0x55)
			{
				user_uart_serial_end_add1(&user_pc_serial_handle);
			}
			else
			{
				break;
			}
		}while(data_recv_len_tmp);
		
		
		data_recv_len_tmp=0;
		memset(user_pc_recvbuff_tmp,0,sizeof(user_pc_recvbuff_tmp));
		data_recv_len_tmp=user_uart_serial_check_data(&user_pc_serial_handle,user_pc_recvbuff_tmp,2);
		if(data_recv_len_tmp==2)
		{
			if(user_pc_recvbuff_tmp[1]==0xaa)
			{
				data_recv_len_tmp=0;
				memset(user_pc_recvbuff_tmp,0,sizeof(user_pc_recvbuff_tmp));
				data_recv_len_tmp=user_uart_serial_check_data(&user_pc_serial_handle,user_pc_recvbuff_tmp,4);
				data_recv_payload_len_tmp=user_pc_recvbuff_tmp[3]+4;
				if(user_uart_serial_get_datalength(&user_pc_serial_handle)>=data_recv_payload_len_tmp)
				{
					memset(user_pc_recvbuff_tmp,0,sizeof(user_pc_recvbuff_tmp));
					user_uart_serial_get_data(&user_pc_serial_handle,user_pc_recvbuff_tmp,data_recv_payload_len_tmp);
					crc_tmp=CRC16 ((const uint8_t *)user_pc_recvbuff_tmp,data_recv_payload_len_tmp-2);
					if((user_pc_recvbuff_tmp[data_recv_payload_len_tmp-2]==crc_tmp/256)
						&&(user_pc_recvbuff_tmp[data_recv_payload_len_tmp-1]==crc_tmp%256))
					{
						printf("pc crc is ok %d %d\r\n",tmp->central_control_address,user_pc_recvbuff_tmp[2]);
						if(user_pc_recvbuff_tmp[2]==tmp->central_control_address)
						{
							printf("pc command is %d\r\n",user_pc_recvbuff_tmp[4]);
							switch(user_pc_recvbuff_tmp[4])
							{
								case PC_READ_DATA://读数据
									pc_recv_flag=1;
								break;
								case PC_SET_TIME_DATA://设置腐蚀仪器时间
									
									user_fushi_set_fushi_status( FUSHI_SET_DEVICE_TIME,0,&(user_pc_recvbuff_tmp[5]));
								break;
								case  PC_SET_CHECK_DATA://设置采集时间
								
									user_fushi_set_fushi_status( FUSHI_SET_CHECK_TIME,user_pc_recvbuff_tmp[5]*256+user_pc_recvbuff_tmp[6],NULL);
								break;
								case PC_SET_PRO://阴极保护开关
									if(user_pc_recvbuff_tmp[5])
									{
										user_fushi_set_fushi_status( FUSHI_SET_POR_OPEN,0,NULL);
									}
									else
									{
										user_fushi_set_fushi_status( FUSHI_SET_POR_CLOSE,0,NULL);
									}
									
								break;
								case PC_SET_DEVICE1_PRO://探头1是否极化
									if(user_pc_recvbuff_tmp[5])
									{
										user_fushi_set_fushi_status( FUSHI_SET_DEVICE1_OPEN,0,NULL);
									}
									else
									{
										user_fushi_set_fushi_status( FUSHI_SET_DEVICE1_CLOSE,0,NULL);
									}
								break;
								case PC_SET_DEVICE2_PRO://探头2是否极化
									if(user_pc_recvbuff_tmp[5])
									{
										user_fushi_set_fushi_status( FUSHI_SET_DEVICE2_OPEN,0,NULL);
									}
									else
									{
										user_fushi_set_fushi_status( FUSHI_SET_DEVICE2_CLOSE,0,NULL);
									}
								break;
								case PC_SET_RESET://reset
									user_fushi_set_fushi_status( FUSHI_SET_DEVICE_RESET,0,NULL);
								break;
							
								
								
								
							}
						}
					}
				}
			}
			else
			{
				user_uart_serial_get_data(&user_pc_serial_handle,user_pc_recvbuff_tmp,2);
			}
		}
	}
	return 0xff;
}


